from multiprocessing import Process,Queue
import requests
url_dic = {
    'cnblogs':'https://www.cnblogs.com/Eva-J/articles/8253549.html',
    'douban':'https://www.douban.com/doulist/1596699/',
    'baidu':'https://www.baidu.com',
    'gitee':'https://gitee.com/old_boy_python_stack__22/teaching_plan/issues/IXSRZ',
}

def producer(name,url,q):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    }
    ret = requests.get(url, headers=headers)
    q.put((name,ret.text))

def consumer(q):
    while True:
        tup=q.get()
        if tup is None:break
        with open(f'{tup[0]}.html',encoding='utf-8',mode='w') as f:
            f.write(tup[1])

if __name__ == '__main__':
    q=Queue()
    p1=[]
    for key in url_dic:
        p=Process(target=producer,args=(key,url_dic[key],q))
        p.start()
        p1.append(p)
    Process(target=consumer,args=(q,)).start()
    for p in p1:p.join()
    q.put(None)
    # for i in range(4):
    #     print(q.get())





















